using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
using System.Text;
using System.IO;

using InfFront.UICore;

namespace InfFront.InfoSources.Base
{
    public partial class ObjectListInfoSource : AbstractListInfoSource
    {
        #region Private Variables
        private static readonly object EVENT_GETLIST = new object();
        private static readonly object EVENT_CREATE_EMPTY_ITEM = new object();
        #endregion

        #region Construction/Initialization
        public ObjectListInfoSource()
        {
            Init();
        }

        public ObjectListInfoSource(IContainer container)
        {
            container.Add(this);
            Init();
        }

        private void Init()
        {
            InitializeComponent();
        }
        #endregion

        public class GetListEventArgs: EventArgs
        {
            public IList List { get; set; }
        }
        public delegate void GetListEventHandler(object sender, GetListEventArgs args);

        public event GetListEventHandler OnGetList
        {
            add { Events.AddHandler(EVENT_GETLIST, value); }
            remove { Events.RemoveHandler(EVENT_GETLIST, value); }
        }

        protected sealed override IList GetInternalList()
        {
            var args = new GetListEventArgs();
            var handler = (GetListEventHandler)Events[EVENT_GETLIST];
            if (handler != null)
                handler(this, args);
            return args.List;
        }

        public class CreateEmptyItemEventArgs : EventArgs
        {
            public object Item { get; set; }
        }
        public delegate void CreateEmptyItemEventHandler(object sender, CreateEmptyItemEventArgs args);

        public event CreateEmptyItemEventHandler OnCreateEmptyItem
        {
            add { Events.AddHandler(EVENT_CREATE_EMPTY_ITEM, value); }
            remove { Events.RemoveHandler(EVENT_CREATE_EMPTY_ITEM, value); }
        }

        protected override object CreateEmptyItem()
        {
            var args = new CreateEmptyItemEventArgs();
            var handler = (CreateEmptyItemEventHandler)Events[EVENT_CREATE_EMPTY_ITEM];
            if (handler != null)
                handler(this, args);
            return args.Item;
        }
    }
}
